package com.edu.study.base.wordLibrary.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.edu.study.base.stuQuestionRelation.domain.StuQuestionRelationRy;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.edu.study.base.wordLibrary.domain.wordLibrary;
import com.edu.study.base.wordLibrary.service.IwordLibraryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * wordLibraryController
 * 
 * @author zw
 * @date 2021-06-26
 */
@RestController
@RequestMapping("/wordLibrary/wordLibrary")
public class wordLibraryController extends BaseController
{
    @Autowired
    private IwordLibraryService wordLibraryService;

    /**
     * 查询wordLibrary列表
     */
    @PreAuthorize("@ss.hasPermi('wordLibrary:wordLibrary:list')")
    @GetMapping("/list")
    public TableDataInfo list(wordLibrary wordLibrary)
    {
        startPage();
        List<wordLibrary> list = wordLibraryService.selectwordLibraryList(wordLibrary);
        return getDataTable(list);
    }

    @GetMapping("/selectCourseWord")
    public TableDataInfo selectCourseWord(wordLibrary wordLibrary)
    {
        startPage();
        List<wordLibrary> list = wordLibraryService.selectCourseWord(wordLibrary);
        return getDataTable(list);
    }

    /**
     * 下载导入模板
     * @return
     */
    @GetMapping("/downloadTemplate")
    public AjaxResult downloadTemplate()
    {
        ExcelUtil<wordLibrary> util = new ExcelUtil<wordLibrary>(wordLibrary.class);
        return util.importTemplateExcel("单词导入模板");
    }

    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<wordLibrary> util = new ExcelUtil<wordLibrary>(wordLibrary.class);
        List<wordLibrary> wordList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = wordLibraryService.importWord(wordList.stream().filter(i -> StringUtils.isNotBlank(i.getWord())).collect(Collectors.toList()), updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导出wordLibrary列表
     */
    @PreAuthorize("@ss.hasPermi('wordLibrary:wordLibrary:export')")
    @Log(title = "wordLibrary", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(wordLibrary wordLibrary)
    {
        List<wordLibrary> list = wordLibraryService.selectwordLibraryList(wordLibrary);
        ExcelUtil<wordLibrary> util = new ExcelUtil<wordLibrary>(wordLibrary.class);
        return util.exportExcel(list, "wordLibrary");
    }

    /**
     * 获取wordLibrary详细信息
     */
    @PreAuthorize("@ss.hasPermi('wordLibrary:wordLibrary:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(wordLibraryService.selectwordLibraryById(id));
    }

    /**
     * 新增wordLibrary
     */
    @PreAuthorize("@ss.hasPermi('wordLibrary:wordLibrary:add')")
    @Log(title = "wordLibrary", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody wordLibrary wordLibrary)
    {

        // 获取单词信息
        wordLibrary word = this.wordLibraryService.selectwordLibraryByName(wordLibrary.getWord());
        if (word != null) return AjaxResult.error("单词已存在");

        wordLibrary = (wordLibrary) BeanUtils.initBeanPublicProperties(wordLibrary,"add");
        return toAjax(wordLibraryService.insertwordLibrary(wordLibrary));
    }

    /**
     * 修改wordLibrary
     */
    @PreAuthorize("@ss.hasPermi('wordLibrary:wordLibrary:edit')")
    @Log(title = "wordLibrary", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody wordLibrary wordLibrary)
    {
        // 获取单词信息
        wordLibrary word = this.wordLibraryService.selectwordLibraryByName(wordLibrary.getWord());
        if (word != null && !word.getId().equals(wordLibrary.getId())) return AjaxResult.error("单词已存在");

        return toAjax(wordLibraryService.updatewordLibrary(wordLibrary));
    }

    /**
     * 删除wordLibrary
     */
    @PreAuthorize("@ss.hasPermi('wordLibrary:wordLibrary:remove')")
    @Log(title = "wordLibrary", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(wordLibraryService.deletewordLibraryByIds(ids));
    }
}
